package com.mo.ld.activity.view.chart

import android.graphics.Color
import android.graphics.Typeface
import com.github.mikephil.charting.charts.Chart
import com.github.mikephil.charting.components.AxisBase
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.LegendEntry
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.data.BarData
import com.github.mikephil.charting.data.BarDataSet
import com.github.mikephil.charting.data.BarEntry
import com.github.mikephil.charting.formatter.ValueFormatter
import com.mo.ld.databinding.ActBarchartBinding
import com.mo.lib.R
import com.mo.lib.ui.activity.BaseVbActivity
import com.mo.lib.utils.KLog
import com.mo.lib.view.chart.*

/**
 * @ author：mo
 * @ data：2022/8/24:14:24
 * @ 功能：
 * https://blog.csdn.net/qq_43332570/article/details/103335457
 */
class BarChartActivity() : BaseVbActivity<ActBarchartBinding>() {
    override fun initData() {

        viewBinding.hbvChart3.setDescription2() {}.setLegend2 {
            //            isEnabled = false
            val legendEntry = LegendEntry() //自定义图例：设置后会覆盖原生图例，且设置后不可追加图例
            legendEntry.label="应检查"
            legendEntry.formColor=Color.GREEN
            val legendEntry2 = LegendEntry() //自定义图例：设置后会覆盖原生图例，且设置后不可追加图例
            legendEntry2.label="已检查"
            legendEntry2.formColor=Color.RED
            setCustom(mutableListOf(legendEntry,legendEntry2 ))
            horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER //水平方向位置：   LEFT, CENTER, RIGHT    默认LEFT
            yOffset=20f
        }.setChartSetting2 {}.setXAxis2 {
            setTextSize(6f);
//            setAxisMaximum(0.6f);
            //把最小值设置为负数能够为下方留出点空白距离
            setAxisMinimum(-0.6f);
            //不显示网格
            setDrawGridLines(false);
            setDrawAxisLine(true);
            //将x轴显示在左侧
            setPosition(XAxis.XAxisPosition.BOTH_SIDED);
            setLabelCount(11);
            //自定义样式
            setValueFormatter(object :ValueFormatter(){
                override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                    return  (value * 10 ).toString() +  "-" + (value + 1) * 10;
                }
            });
        }.setAxisLeft2 {
            isEnabled = false
            setAxisMinimum(-25f);
            setAxisMaximum(25f);
        }.setAxisRight2 {
            setTextSize(6f);
            setAxisMinimum(-25f);
            setAxisMaximum(25f);
            setTextColor(Color.BLACK);
            setDrawGridLines(false);
            setDrawLabels(true);
            setValueFormatter(object : ValueFormatter() {
                override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                    return Math.abs(value).toString() + " MM";
                }
            })
        }.loadBarData(arrayListOf(BarEntry(0f, floatArrayOf(11f, 11f)), BarEntry(1f, floatArrayOf(10f, 10f)), BarEntry(2f, floatArrayOf(9f, 9f)), BarEntry(3f, floatArrayOf(8f, 8f))), ""
            , barDataSet = {
                setColors( resources.getColor(R.color.text_green), resources.getColor(R.color.text_red))//依次设置每次柱块的颜色
            }
            , barData = {
                barWidth = 0.3f //设置柱块的宽度
                setValueFormatter(object : ValueFormatter() {
                    override fun getBarLabel(barEntry: BarEntry?): String {
                        //                        return String.format("%.2f", barEntry!!.y) + "%" //保留两位小数
                        return ""
                    }
                })
                setValueTextColor(Color.GREEN)
                setValueTextSize(6f)
                setValueTypeface(Typeface.DEFAULT_BOLD)
                KLog.i(dataSets.size.toString())
            }
        )






        viewBinding.hbvChart.setDescription2() {}.setLegend2 {
            isEnabled = false
        }.setChartSetting2 {}.setXAxis2 {
                textSize = 12f //文本大小
                position = XAxis.XAxisPosition.BOTTOM //设置X轴所在位置   默认为上面,值：BOTTOM,BOTH_SIDED,BOTTOM_INSIDE,TOP,TOP_INSID
                labelCount = 4 //X轴标签数量
                setDrawGridLines(false) //是否绘制X轴上的网格线（背景里面的竖线 HorizontalBarChart里的横线）
                valueFormatter = object : ValueFormatter() {
                    override fun getAxisLabel(value: Float, axis: AxisBase?): String { //设置标签显示内容
                        return arrayOf("燃气经营", "燃气运输", "燃气用户", "燃气具")[value.toInt()] //注意这里不需要显示的要改成 ""
                    }
                }
                axisLineColor = Color.rgb(245, 133, 179); //X轴颜色
                axisLineWidth = 2f;   //X轴粗细
            }.setAxisLeft2 {
                isEnabled = false
            }.setAxisRight2 {
                textSize = 12f
                axisMinimum = 0f
                setDrawGridLines(false)
                setValueFormatter(object : ValueFormatter() {
                    override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                        return value.toInt().toString()
                    }
                })
            }.loadBarData(arrayListOf(BarEntry(0f, 2.51f), BarEntry(1f, 3.51f), BarEntry(2f, 5.51f), BarEntry(3f, 7.51f)), "") {
                barWidth = 0.3f //设置柱块的宽度
                setValueFormatter(object : ValueFormatter() {
                    override fun getBarLabel(barEntry: BarEntry?): String {
                        return String.format("%.2f", barEntry!!.y) + "%" //保留两位小数
                    }
                })
                setValueTextColor(Color.GREEN)
                setValueTextSize(12f)
                setValueTypeface(Typeface.DEFAULT_BOLD)
                (dataSets[0] as BarDataSet).setColors(resources.getColor(R.color.blue),
                    resources.getColor(R.color.text_red),
                    resources.getColor(R.color.text_green),
                    resources.getColor(R.color.text_red)) //依次设置每次柱块的颜色
            }



        viewBinding.hbvChart2.setDescription2() {}.setLegend2 {
//            isEnabled = false
            val legendEntry = LegendEntry() //自定义图例：设置后会覆盖原生图例，且设置后不可追加图例
            legendEntry.label="应检查"
            legendEntry.formColor=Color.GREEN
            val legendEntry2 = LegendEntry() //自定义图例：设置后会覆盖原生图例，且设置后不可追加图例
            legendEntry2.label="已检查"
            legendEntry2.formColor=Color.RED
            setCustom(mutableListOf(legendEntry,legendEntry2 ))
            horizontalAlignment = Legend.LegendHorizontalAlignment.CENTER //水平方向位置：   LEFT, CENTER, RIGHT    默认LEFT
            stackSpace=-50f
        }.setChartSetting2 {}.setXAxis2 {
                textSize = 12f //文本大小
                position = XAxis.XAxisPosition.BOTTOM //设置X轴所在位置   默认为上面,值：BOTTOM,BOTH_SIDED,BOTTOM_INSIDE,TOP,TOP_INSID
                labelCount = 4 //X轴标签数量
                setDrawGridLines(false) //是否绘制X轴上的网格线（背景里面的竖线 HorizontalBarChart里的横线）
                valueFormatter = object : ValueFormatter() {
                    override fun getAxisLabel(value: Float, axis: AxisBase?): String { //设置标签显示内容
                        return arrayOf("燃气具", "燃气用户", "燃气运输", "燃气经营")[value.toInt()] //注意这里不需要显示的要改成 ""
                    }
                }
                axisLineColor = Color.rgb(245, 133, 179); //X轴颜色
                axisLineWidth = 2f;   //X轴粗细
            }.setAxisLeft2 {
                isEnabled = false
            }.setAxisRight2 {
                textSize = 12f
                axisMinimum = 0f
                setDrawGridLines(false)
                setValueFormatter(object : ValueFormatter() {
                    override fun getAxisLabel(value: Float, axis: AxisBase?): String {
                        return value.toInt().toString()
                    }
                })
            }.loadBarData(arrayListOf(BarEntry(0f, floatArrayOf(11f, 11f)), BarEntry(1f, floatArrayOf(10f, 10f)), BarEntry(2f, floatArrayOf(9f, 9f)), BarEntry(3f, floatArrayOf(8f, 8f))), ""
        , barDataSet = {
                setColors( resources.getColor(R.color.text_green), resources.getColor(R.color.text_red))//依次设置每次柱块的颜色
            }
        , barData = {
                barWidth = 0.3f //设置柱块的宽度
                setValueFormatter(object : ValueFormatter() {
                    override fun getBarLabel(barEntry: BarEntry?): String {
//                        return String.format("%.2f", barEntry!!.y) + "%" //保留两位小数
                        return ""
                    }
                })
                setValueTextColor(Color.GREEN)
                setValueTextSize(12f)
                setValueTypeface(Typeface.DEFAULT_BOLD)
                KLog.i(dataSets.size.toString())
            }
        )

    }



}